//
//  main.m
//  5-9学习
//
//  Created by 钟纪旺 on 16/5/9.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import <Foundation/Foundation.h>
#include "PhoneData.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSString *path = @"/Users/zhongjiwang/Desktop/cbv3.vcf";
        NSError *err;
        NSString *vcf = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        if (err !=0) {
            NSLog(@"%@",err);
        }
//        NSLog(@"%@",vcf);
        PhoneData *item;
        NSMutableArray *d= [NSMutableArray new];
        NSArray *arr =[vcf componentsSeparatedByString:@"\n"];
        for (NSString *line in arr) {
            if ([line hasPrefix:@"BEGIN"]) {
                item = [PhoneData new];
                NSLog(@"开始");
            }
            if ([line hasPrefix:@"END"]){
                [d addObject:item];
                NSLog(@"结束");
            
            }
            if ([line hasPrefix:@"TEL"]){
            
                //NSLog(@"找到一个电话%@",line);
                NSArray *tels =[line componentsSeparatedByString:@";"];
                NSString *last = tels.lastObject;
                last = [last substringFromIndex:5];
                NSLog(@"tel:%@",last);
                
                
                
            }
            if ([line hasPrefix:@"FN"]) {
//                NSArray *upperComponents = [line componentsSeparatedByString:@":"];
//                NSString *name = upperComponents[1];
//                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
//                name = [name stringByRemovingPercentEncoding];
//                NSLog(@"名字:%@",name);
//            }
                NSArray *names = [line componentsSeparatedByString:@":"];
                NSString *name =names.lastObject;
                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                name = name.stringByRemovingPercentEncoding;
                //percent 百分号
                NSLog(@"名字:%@",name);
            }
//            }if([line hasPrefix:@"N"]){
//            
//                NSLog(@"%@",line);
//                NSArray *upperComponents = [line componentsSeparatedByString:@":"];
//                NSLog(@"%@",upperComponents);
//                NSArray *components = [[upperComponents objectAtIndex:1] componentsSeparatedByString:@";"];
//                
//                NSString * lastName = [components objectAtIndex:0];
//                NSLog(@"%@",lastName);
//                NSString * firstName = [components objectAtIndex:1];
//                NSLog(@"%@",firstName);
//                lastName = [lastName stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
//                NSLog(@"%@",lastName);
//                //replacing 替换 occurrences出现
//                firstName = [firstName stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
//                NSLog(@"%@",firstName);
//            firstName = firstName.stringByRemovingPercentEncoding;
//            }
            
        
        }
      
    }
    
    return 0;
}


//第二种方法
/*
 //
 //  main.m
 //  5-9学习
 //
 //  Created by 钟纪旺 on 16/5/9.
 //  Copyright © 2016年 zjw. All rights reserved.
 //
 
 #import <Foundation/Foundation.h>
 #include "PhoneData.h"
 int main(int argc, const char * argv[]) {
 @autoreleasepool {
 // insert code here...
 NSString *path = @"/Users/zhongjiwang/Desktop/cbv.vcf";
 NSError *err;
 
 NSString *vcf = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
 if (err !=0) {
 NSLog(@"%@",err);
 }
 //        NSLog(@"%@",vcf);
 PhoneData *item;
 NSMutableArray *d= [NSMutableArray new];
 NSArray *arr =[vcf componentsSeparatedByString:@"\n"];
 for (NSString *line in arr) {
 if ([line hasPrefix:@"BEGIN"]) {
 item = [PhoneData new];
 
 //  NSLog(@"开始");
 }
 if ([line hasPrefix:@"END"]){
 [d addObject:item];
 //    NSLog(@"结束");
 
 }
 if ([line hasPrefix:@"TEL"]){
 
 NSArray *components = [line componentsSeparatedByString:@":"];
 NSString *phoneNumber = [components objectAtIndex:1];
 
 item.tel= [phoneNumber substringToIndex:11];
 NSLog(@"%@",item.tel);
 //  NSLog(@"找到一个电话");
 }
 if ([line hasPrefix:@"FN"]) {
 NSArray *upperComponents = [line componentsSeparatedByString:@":"];
 NSString *name = upperComponents[1];
 name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
 name = [name stringByRemovingPercentEncoding];
 // removing 删除 percent 百分比 encoding 编码
 item.name = name;
 //  NSLog(@"找到一个名字");
 NSLog(@"%@",item.name);
 }
 }
 }
 return 0;
 }
 */

